﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Program_FlightChess
{
    #region 棋盘逻辑
    class ChessBuild
    {
        #region 棋盘对象
        public string chessString = "□";
        public int ChessBoardLength = 90;
        public bool isWin = false;
        ChessBoard cb;
        #endregion

        #region 玩家对象
        Player player;
        string PlayerName = "Komeiji";
        Player bot1;
        Player bot2;
        List<Player> players;
        int playerTempCount = 0;
        #endregion

        #region 棋盘事件对象
        List<ChessBoardAction> cbaList;
        string cbEventMessage = "";
        ChessBoardAction Boom;
        public string boomString = "☆";
        public const int boomCount = 3;
        public int[] boomIndexArr;

        ChessBoardAction Lucky;
        public string luckyString = "◎";
        public const int luckyCount = 6;
        public int[] luckyIndexArr;

        ChessBoardAction Stop;
        public string stopString = "▲";
        public const int stopCount = 3;
        public int[] stopIndexArr;

        ChessBoardAction Tunnel;
        public string tunnelString = "卐";
        public const int tunnelCount = 4;
        public int[] tunnelIndexArr;

        ChessBoardAction PlayerWin;
        #endregion

        /// <summary>
        /// 构造函数，用于初始化对象
        /// </summary>
        public ChessBuild()
        {
            #region 棋盘对象初始化
            cb = new ChessBoard(chessString, ChessBoardLength);
            #endregion

            #region 玩家对象初始化

            Console.WriteLine("请输入玩家姓名");
            PlayerName = Console.ReadLine();
            while (PlayerName == "")
            {
                Console.WriteLine("请输入正确的玩家姓名");
                PlayerName = Console.ReadLine();
            }
            Console.WriteLine("宁的姓名为:" + PlayerName);

            player = new Player(PlayerName, "");
            player.PlayerIndex = 0;
            bot1 = new Player("波特", "");
            bot1.PlayerIndex = 0;


            players = new List<Player>();
            players.Add(player);
            players.Add(bot1);
            //players.Add(bot2);

            #endregion

            #region 棋盘事件对象初始化
            cbaList = new List<ChessBoardAction>();

            PlayerWin = new ChessBoardAction("", null);
            PlayerWin.OnTrigger += OnPlayerWinEvent;

            boomIndexArr = new int[] { 6, 46, 71 };
            //boomIndexArr = new int[] {  71 };
            Boom = new ChessBoardAction(boomString, boomIndexArr);
            Boom.OnTrigger += OnBoomEvent;
            cbaList.Add(Boom);

            luckyIndexArr = new int[] { 3, 13, 18, 51, 69, 88 };
            //luckyIndexArr = new int[] { 88 };
            Lucky = new ChessBoardAction(luckyString, luckyIndexArr);
            Lucky.OnTrigger += OnLuckyEvent;
            cbaList.Add(Lucky);

            stopIndexArr = new int[] { 20, 38, 75, 2, 4, 5 };
            //stopIndexArr = new int[] {  75 };
            Stop = new ChessBoardAction(stopString, stopIndexArr);
            Stop.OnTrigger += OnStopEvent;
            cbaList.Add(Stop);

            tunnelIndexArr = new int[] { 24, 44, 65, 78 };
            //tunnelIndexArr = new int[] { 78 };
            Tunnel = new ChessBoardAction(tunnelString, tunnelIndexArr);
            Tunnel.OnTrigger += OnTunnelEvent;
            cbaList.Add(Tunnel);
            #endregion

            foreach (var item in cbaList)//替换地图字符串
            {
                if (item.chessBoardIndex != null)
                {
                    foreach (var cba in item.chessBoardIndex)
                    {
                        cb.chessMap[cba] = item.ActionString[0];
                    }
                }
            }
        }

        /// <summary>
        /// 输出整个棋盘的函数，这个程序的核心
        /// </summary>
        /// <returns></returns>
        public string ChessBoardPrint()
        {
            string Message = "";//棋盘信息

            if (isWin == false)//是否胜利
            {

                #region 棋盘打印
                Console.Clear();
                #region 游戏提示
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.Write("\n" + "Blue");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write("表示为玩家,");
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.Write("Magenta");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.Write("表示为电脑" + "\n" + "\n");
                #endregion

                int temp = 0;
                for (int w = 0; w < 5; w++)
                {
                    for (int j = 0; j < cb.chessMap.Count / 5; j++)
                    {
                        #region 棋盘颜色处理            
                        foreach (var item in cbaList)
                        {
                            foreach (var pla in players)
                            {
                                if (pla.PlayerIndex == temp)
                                {
                                    Console.ForegroundColor = ConsoleColor.Blue;
                                }
                                if (pla.PlayerIndex == temp && pla.playerName != PlayerName)
                                {
                                    Console.ForegroundColor = ConsoleColor.Magenta;
                                }
                            }


                            if (item.actionName == boomString)
                            {
                                foreach (var item1 in item.chessBoardIndex)
                                {
                                    if (temp == item1)
                                    {
                                        Console.ForegroundColor = ConsoleColor.Gray;
                                    }
                                }
                            }
                            else if (item.actionName == luckyString)
                            {
                                foreach (var item1 in item.chessBoardIndex)
                                {
                                    if (temp == item1)
                                    {
                                        Console.ForegroundColor = ConsoleColor.Green;
                                    }
                                }
                            }
                            else if (item.actionName == stopString)
                            {
                                foreach (var item1 in item.chessBoardIndex)
                                {
                                    if (temp == item1)
                                    {
                                        Console.ForegroundColor = ConsoleColor.Red;
                                    }
                                }
                            }
                            else if (item.actionName == tunnelString)
                            {
                                foreach (var item1 in item.chessBoardIndex)
                                {
                                    if (temp == item1)
                                    {
                                        Console.ForegroundColor = ConsoleColor.White;
                                    }
                                }
                            }

                        }

                        #endregion

                        #region 棋盘打印图形
                        if (w == 0)
                        {
                            Console.Write(cb.chessMap[temp]);
                            temp++;
                        }
                        else if (w == 1)
                        {
                            if (j == 0)
                            {
                                Console.WriteLine();
                            }

                            for (int b = 0; b < (ChessBoardLength / 5) - 1; b++)
                            {

                                Console.Write("  ");
                            }

                            Console.WriteLine(cb.chessMap[temp]);

                            if (j == (ChessBoardLength / 5) - 1)
                            {

                                temp += ((ChessBoardLength / 5) - 1);
                            }
                            temp++;

                        }
                        else if (w == 2)
                        {


                            Console.Write(cb.chessMap[temp]);
                            if (j == (ChessBoardLength / 5) - 1)
                            {
                                temp += ((ChessBoardLength / 5) + 1);
                            }
                            temp--;

                        }
                        else if (w == 3)
                        {
                            if (j == 0)
                            {
                                Console.WriteLine();
                            }

                            Console.WriteLine(cb.chessMap[temp]);
                            temp++;
                        }
                        else if (w == 4)
                        {

                            Console.Write(cb.chessMap[temp]);
                            temp++;
                        }
                        #endregion

                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                }
                #endregion
            }
            Console.WriteLine("\n");
            Message += PlayerBehavior();//调用玩家逻辑函数     
            return Message;
        }

        #region 玩家逻辑

        /// <summary>
        /// 玩家行为函数，负责玩家的行动逻辑
        /// </summary>
        /// <returns>返回玩家行动信息</returns>
        public string PlayerBehavior()
        {
            string playerMessage = "";


            if (players[playerTempCount].playerState == Player.PlayerState.move)
            {
                playerMessage += PlayerMove(players[playerTempCount]);
                Console.WriteLine(players[playerTempCount].playerName + "请输入任意键掷出骰子");
                Console.ReadLine();
                foreach (var item in cbaList)
                {
                    foreach (var item1 in item.chessBoardIndex)
                    {
                        if (players[playerTempCount].PlayerIndex == item1)
                        {
                            item.OnTrigger(players[playerTempCount]);
                            playerMessage += cbEventMessage;
                            cbEventMessage = "";
                        }
                    }
                }
            }

            else
            {
                Console.WriteLine(players[playerTempCount].playerName + "在空间与时间中模糊了自己的存在");
                players[playerTempCount].playerState = Player.PlayerState.move;

                playerMessage += cbEventMessage;
                cbEventMessage = "";
            }

            if (playerTempCount < players.Count - 1)
            {
                playerTempCount++;
            }
            else if (playerTempCount == players.Count - 1)
            {
                playerTempCount = 0;
            }
            foreach (var item in players)
            {
                if (item.PlayerIndex > ChessBoardLength)
                {
                    PlayerWin.OnTrigger(item);
                    return "";
                }

            }
            return playerMessage;
        }
        public int Dice()
        {
            return new Random().Next(1, 7);
        }
        public string PlayerMove(Player player)
        {
            int i = Dice();
            player.PlayerIndex += i;
            return "\n" + "玩家" + player.playerName + "掷出了骰子" + "\n" + "移动了 " + i + " 格" + "\n";
        }
        #endregion

        #region 事件
        void OnPlayerWinEvent(Player ePlayer)
        {
            isWin = true;
            Console.Clear();
            string winMessage = ePlayer.playerName + " 取得了胜利!";
            foreach (var item in winMessage)
            {
                Console.Write(item);
                System.Threading.Thread.Sleep(100);
            }
            Console.ReadLine();
        }
        void OnBoomEvent(Player ePlayer)
        {
            ePlayer.MoveBack(6);
            cbEventMessage = "玩家" + ePlayer.playerName + "踩到到了炸弹，炸弹炸了，后退6格";
        }
        void OnLuckyEvent(Player ePlayer)
        {
            cbEventMessage = "玩家" + ePlayer.playerName + "遇到了幸运轮盘事件";
            int i = 0;
            Console.WriteLine("\n" + "\n" + "玩家" + ePlayer.playerName + "拿到了幸运轮盘");
            Console.ReadLine();
            Console.WriteLine("请输入1或者2,1为选择一个对手交换位置,2为选择一个对手后退六格");
            while (!int.TryParse(Console.ReadLine(), out i))
            {
                Console.WriteLine("请输入1或2");
            }
            if (i == 1)
            {
                Console.Write("\n" + "请输入对手的Name(");
                foreach (var item in players)
                {
                    Console.Write(" " + item.playerName + " ");
                }
                Console.Write(")" + "\n");

                bool isBreak = false;
                string luckyBoy = Console.ReadLine();
                while (!isBreak)
                {
                    foreach (var item in players)
                    {
                        if (luckyBoy == item.playerName && luckyBoy != ePlayer.playerName)
                        {
                            int indextemp;
                            indextemp = ePlayer.PlayerIndex;
                            ePlayer.PlayerIndex = item.PlayerIndex;
                            item.PlayerIndex = indextemp;
                            Console.WriteLine("幸运儿 " + item.playerName + " 与玩家" + ePlayer.playerName + " 交换了位置");
                            isBreak = true;
                            break;
                        }
                        else if (luckyBoy == ePlayer.playerName)
                        {
                            Console.WriteLine("我换我自己？行吧");
                            Console.ReadLine();
                            Console.WriteLine("幸运儿 " + ePlayer.playerName + " 与玩家" + ePlayer.playerName + " 交换了位置");
                            isBreak = true;
                            break;
                        }
                    }
                }
            }
            else if (i == 2)
            {
                Console.WriteLine("请输入对手的Name(");
                foreach (var item in players)
                {
                    Console.Write(" " + item.playerName + " ");
                }
                Console.Write(")" + "\n");

                bool isBreak = false;
                string luckyBoy = Console.ReadLine();
                while (!isBreak)
                {
                    foreach (var item in players)
                    {
                        if (luckyBoy == item.playerName && luckyBoy != ePlayer.playerName)
                        {
                            item.MoveBack(6);
                            Console.WriteLine("幸运儿 " + item.playerName + " 后退了6格");
                            isBreak = true;
                            break;
                        }
                        else if (luckyBoy == ePlayer.playerName)
                        {
                            Console.WriteLine("噢，你可真是个带慈善家，行吧！");
                            Console.ReadLine();
                            Console.WriteLine("玩家" + ePlayer.playerName + " 后退了六格");
                            Console.WriteLine("玩家" + ePlayer.playerName + " 获得了称号“慈善家”");
                            isBreak = true;
                            break;
                        }
                    }
                }
            }
        }
        void OnTunnelEvent(Player ePlayer)
        {

            ePlayer.MoveFront(10);
            cbEventMessage = "玩家" + ePlayer.playerName + "进入了时刻隧道，前进10格";
        }
        void OnStopEvent(Player ePlayer)
        {
            ePlayer.playerState = Player.PlayerState.stop;
            cbEventMessage = "玩家" + ePlayer.playerName + "被停止了时间,暂停一回合";
        }
        #endregion
    }
    #endregion

}
